package com.dhcc.sdc.cdss.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.RedisUtil;
import com.dhcc.sdc.base.entity.SdcQcCdssLinkEntity;
import com.dhcc.sdc.base.service.ICdssStandardMatchService;
import com.dhcc.sdc.base.service.ISdcQcCdssLinkService;
import com.dhcc.sdc.busi.entity.SdcQcEntity;
import com.dhcc.sdc.busi.service.ISdcQcService;
import com.dhcc.sdc.cdss.dao.SdcCdssNursingMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssNursingEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssNursingService;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceFieldEntity;
import com.dhcc.sds.standard.service.ISdsStandSourceFieldService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


/**
 * 表数据服务层接口
 * 
 * @ClassName: SdcCdssNursingServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-06-23 15:27:31
 */
@Service("sdcCdssNursingService")
public class SdcCdssNursingServiceImpl extends BaseServiceImpl<SdcCdssNursingMapper, SdcCdssNursingEntity> implements ISdcCdssNursingService {

	@Autowired
	private ISdcQcService sdcQcService;
	@Autowired
	private ISdsStandSourceService standSourceService;
	@Autowired
	private ISdsStandSourceFieldService sdsStandSourceFieldService;
	@Autowired
	private ICdssStandardMatchService cdssStandardMatchService;
	@Autowired
	private ISdcQcCdssLinkService sdcQcCdssLinkService;

	@Override
	public List<SdcCdssNursingEntity> page(Page<SdcCdssNursingEntity> page, Map<String, Object> map) {
		List<SdcCdssNursingEntity> page1 = this.baseMapper.page(page, map);
		for (SdcCdssNursingEntity cdssNursingEntity : page1){
			SdcQcCdssLinkEntity sdcQcCdssLinkEntity = sdcQcCdssLinkService.selectByNodeId(cdssNursingEntity.getNodeId());
			if (!StringUtils.isEmpty(sdcQcCdssLinkEntity)){
				SdcQcEntity sdcQcEntity = sdcQcService.selectById(sdcQcCdssLinkEntity.getQcItemId());
				if (!StringUtils.isEmpty(sdcQcEntity)){
					cdssNursingEntity.setQcDesc(sdcQcEntity.getQcDesc());
				}
			}
			if (!StringUtils.isEmpty(cdssNursingEntity.getDataSource()) && !StringUtils.isEmpty(cdssNursingEntity.getDataSourceItem())){
				/*SdsStandSourceEntity sourceEntity = standSourceService.selectById(cdssNursingEntity.getDataSource());
				SdsStandSourceFieldEntity sourceFieldEntity = sdsStandSourceFieldService.selectById(cdssNursingEntity.getDataSourceItem());
				if (!StringUtils.isEmpty(sourceEntity) && !StringUtils.isEmpty(sourceFieldEntity)){
					cdssNursingEntity.setReferenceTime(sourceEntity.getSourceDesc() + "-" + sourceFieldEntity.getFieldDesc());
				}*/
				String referenceTime = cdssStandardMatchService.handleReferenceTime(SdcCdssNursingEntity.class, cdssNursingEntity);
				cdssNursingEntity.setReferenceTime(referenceTime);
			}
		}
		return page1;
	}
	@Override
	public List<SdcCdssNursingEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public String handleJsonArrayData(JSONArray jsonArray, Long qcEntityId, List<String> cdssNursingNodeIds) {
		String res;
		int count = 0;
		List<SdcCdssNursingEntity> cdssNursingEntities = JSONObject.parseArray(jsonArray.toJSONString(), SdcCdssNursingEntity.class);
		for (SdcCdssNursingEntity cdssNursingEntity : cdssNursingEntities){
			SdcCdssNursingEntity cdssNursingEntityNew = new SdcCdssNursingEntity();
			cdssNursingEntityNew.setQcentityId(qcEntityId);
			cdssNursingEntityNew.setItemId(cdssNursingEntity.getItemId());
			cdssNursingEntityNew.setNodeId(cdssNursingEntity.getNodeId());
			cdssNursingNodeIds.add(cdssNursingEntity.getNodeId());
			SdcCdssNursingEntity cdssNursingEntity1 = this.baseMapper.selectOne(cdssNursingEntityNew);
			if (StringUtils.isEmpty(cdssNursingEntity1)){
				//insert
				cdssNursingEntity.setQcentityId(qcEntityId);
				cdssNursingEntity.setIsCdss(1);
				boolean insert = cdssNursingEntity.insert();
				if (insert){
					count++;
				}
			}else {
				//update
				cdssNursingEntity1.setQcentityId(qcEntityId);
				cdssNursingEntity1.setItemDesc(cdssNursingEntity.getItemDesc());
				cdssNursingEntity1.setItemName(cdssNursingEntity.getItemName());
				cdssNursingEntity1.setSourceFlag(cdssNursingEntity.getSourceFlag());
				cdssNursingEntity1.setNoCondition(cdssNursingEntity.getNoCondition());
				cdssNursingEntity1.setCureId(cdssNursingEntity.getCureId());
				boolean b = cdssNursingEntity1.updateAllColumnById();
				if (b){
					count++;
				}
			}
		}
		res = "新增/更新" + count + "条";
		return res;
	}

	@Override
	public List<Map<String, Object>> sdcItemList(Map<String, Object> map) {
		return this.baseMapper.sdcItemList(map);
	}

	@Override
	public List<String> selectNodeIds(Long entityId) {
		return this.baseMapper.selectNodeIds(entityId);
	}

	@Override
	public void deleteByNodeIds(List<String> nodeIds) {
		this.baseMapper.deleteByNodeIds(nodeIds);
	}


}